package com.sunshine.model;

import com.sunshine.bean.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by along on 11/18/17.
 */
@Mapper
public interface ArticleMapper {

    @Insert("INSERT INTO articles(id,title, content, type,attachment,view_count,created_date) "
            + " VALUES(#{id}, #{title}, #{content}, #{type},#{attachment},#{viewCount},now())")
    int insert(Article article);

    @Select("SELECT * FROM articles WHERE id = #{id}")
    @Results(id = "ArticleMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "type", column = "type"),
            @Result(property = "attachment", column = "attachment"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "viewCount", column = "view_count")
    })
    Article findById(int id);

    @Update("UPDATE articles SET view_count = #{viewCount} WHERE id = #{id}")
    int read(@Param(value = "id") int id, @Param(value = "viewCount") int viewCount);

    @Select("SELECT * FROM articles WHERE type=#{type}")
    @ResultMap("ArticleMap")
    List<Article> findByType(@Param(value = "type")String type);


    @Select("SELECT * FROM articles WHERE type = #{type} LIMIT #{n}")
    @ResultMap("ArticleMap")
    List<Article> previewByType(@Param(value = "type")String type,@Param(value = "n")int n);

    @Delete("DELETE FROM articles WHERE id = #{id}")
    int delete(int id);
}
